package com.jcal.librarymanager;

import java.util.Locale;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;

import com.jcal.librarymanager.dialogs.Ajouter;
import com.jcal.librarymanager.fragments.FragmentListe;
import com.jcal.librarymanager.fragments.FragmentParametres;

public class MainActivity extends FragmentActivity {

	SectionsPagerAdapter sectionsPagerAdapter;
	ViewPager viewPager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

		viewPager = (ViewPager) findViewById(R.id.pager);
		viewPager.setAdapter(sectionsPagerAdapter);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(android.view.MenuItem item) {
		
		switch (item.getItemId()) {
		case R.id.action_ajouter:
			Dialog dialog = new Ajouter(this);
			dialog.show();
			return true;
		}
		
		return true;
	};


	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			
			switch (position) {
			case 0:
				FragmentListe fragmentListe = new FragmentListe();
				return fragmentListe;
				
			case 1:
				FragmentParametres fragmentParametres = new FragmentParametres();
				return fragmentParametres;
				
			}
			return null;
		}

		/* 
		 * Nombre de pages dans le switch
		 */
		@Override
		public int getCount() {
			return 2;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			Locale l = Locale.getDefault();
			switch (position) {
			case 0:
				return getString(R.string.titre_section_liste).toUpperCase(l);
			case 1:
				return getString(R.string.titre_section_parametres).toUpperCase(l);
			}
			return null;
		}
	}

}
